package com.qd.marketlist;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class MyProvider extends ContentProvider {
    private static final String TAG = "myProvider";
    private static final boolean DBG = MLConst.DEBUG;

    private static final String DB_NAME = "wishlist.db";
    private static final String DB_TABLE = "mywishlist";
    private static final int DB_VER = 1;

    //private static final int ITEM = 1;
    private static final int ITEMS = 2;
    //private static final int HISTORY = 3;
    private static final int HISTORYS = 4;
    private static final int DRUGITEMS = 5;
    private static final int DRUGHISTORYS = 6;
    private static final String URI_ITEM = "wishlist";
    private static final String URI_DRUGITEM = "drugwishlist";
    private static final String URI_HISTORY = "history";
    private static final String URI_DRUGHISTORY = "drughistory";

    private static final String CREATE_SQL = "create table if not exists "
            + MLConst.DB_TABLE_NAME
            + "("
            + MLConst.DBSTR_COL_ID 
            + " integer primary key autoincrement,"
            + MLConst.DBSTR_COL_NAME
            + " varchar(20) UNIQUE, "
            + MLConst.DBSTR_COL_CHECKBOX_STATE
            + " integer)";

    private static final String CREATE_DRUG = "create table if not exists "
            + MLConst.DB_TABLE_DRUGLIST
            + "("
            + MLConst.DBSTR_COL_ID 
            + " integer primary key autoincrement,"
            + MLConst.DBSTR_COL_NAME
            + " varchar(20) UNIQUE, "
            + MLConst.DBSTR_COL_CHECKBOX_STATE
            + " integer)";

    private static final String CREATE_HISTORY = "create table if not exists "
            + MLConst.DB_TABLE_HISTORY
            + "("
            + MLConst.DBSTR_COL_ID 
            + " integer primary key autoincrement,"
            + MLConst.DBSTR_COL_NAME
            + " varchar(20) UNIQUE, "
            + MLConst.DBSTR_COL_COUNT
            + " integer)";
    private static final String CREATE_DRUGHISTORY = "create table if not exists "
            + MLConst.DB_TABLE_DRUGHISTORY
            + "("
            + MLConst.DBSTR_COL_ID 
            + " integer primary key autoincrement,"
            + MLConst.DBSTR_COL_NAME
            + " varchar(20) UNIQUE, "
            + MLConst.DBSTR_COL_COUNT
            + " integer)";

    private Context mContext;
    private DBHelper mDBHelper;
    private ContentResolver mResolver;
    /*Authority*/
    public static final String AUTHORITY = "com.qd.marketlist";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + URI_ITEM);
    public static final Uri CONTENT_HISTORYURI = Uri.parse("content://" + AUTHORITY + "/" + URI_HISTORY);
    public static final Uri CONTENT_DRUGHISTORY_URI = Uri.parse("content://" + AUTHORITY + "/" + URI_DRUGHISTORY);
    public static final Uri CONTENT_DRUG_URI = Uri.parse("content://" + AUTHORITY + "/" + URI_DRUGITEM);

    private static UriMatcher uriMatcher;

    static { 
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
        uriMatcher.addURI(AUTHORITY, URI_ITEM, ITEMS); 
        //uriMatcher.addURI(AUTHORITY, URI_ITEM + "/#", ITEM);
        uriMatcher.addURI(AUTHORITY, URI_HISTORY, HISTORYS);
        uriMatcher.addURI(AUTHORITY, URI_DRUGHISTORY, DRUGHISTORYS);
        uriMatcher.addURI(AUTHORITY, URI_DRUGITEM, DRUGITEMS);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        int count = 0;
        switch(uriMatcher.match(uri)){
        case ITEMS:{
            count = db.delete(MLConst.DB_TABLE_NAME,
                    selection,
                    selectionArgs);
            break;
        }
        case HISTORYS:{
            count = db.delete(MLConst.DB_TABLE_HISTORY,
                    selection,
                    selectionArgs);
            break;
        }
        case DRUGITEMS:{
            count = db.delete(MLConst.DB_TABLE_DRUGLIST,
                    selection,
                    selectionArgs);
            break;
        }
        case DRUGHISTORYS:{
            count = db.delete(MLConst.DB_TABLE_DRUGHISTORY,
                    selection,
                    selectionArgs);
            break;
        }
        default:{
            if(DBG) Log.e(TAG, "uri error in query()!!!");
            break;
        }
        }
        mResolver.notifyChange(uri, null);
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        Uri newUri = null;
        switch(uriMatcher.match(uri)){
        case ITEMS:{
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            long id = db.insert(MLConst.DB_TABLE_NAME, null, values);
            if( id > 0 ){
                newUri = ContentUris.withAppendedId(uri, id);
                mResolver.notifyChange(uri, null);
            }else{
                if(DBG) Log.e(TAG, "insert error !!!");
            }
            break;
        }
        case HISTORYS:{
        	SQLiteDatabase db = mDBHelper.getWritableDatabase();
        	long id = db.insert(MLConst.DB_TABLE_HISTORY, null, values);
        	if( id > 0 ){
                newUri = ContentUris.withAppendedId(uri, id);
                mResolver.notifyChange(uri, null);
            }else{
                if(DBG) Log.e(TAG, "insert error !!!");
            }
        	break;
        }
        case DRUGHISTORYS:{
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            long id = db.insert(MLConst.DB_TABLE_DRUGHISTORY, null, values);
            if( id > 0 ){
                newUri = ContentUris.withAppendedId(uri, id);
                mResolver.notifyChange(uri, null);
            }else{
                if(DBG) Log.e(TAG, "insert error !!!");
            }
            break;
        }
        case DRUGITEMS:{
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            long id = db.insert(MLConst.DB_TABLE_DRUGLIST, null, values);
            if( id > 0 ){
                newUri = ContentUris.withAppendedId(uri, id);
                mResolver.notifyChange(uri, null);
            }else{
                if(DBG) Log.e(TAG, "insert error !!!");
            }
            break;
        }
        default:
            break;
        }
        return newUri;
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        mContext = getContext();
        mResolver = mContext.getContentResolver();
        mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VER);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        //SQLiteDatabase db = mDBHelper.getReadableDatabase();
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        Cursor c = null;
        switch(uriMatcher.match(uri)){
        case HISTORYS:{
        	c = db.query(MLConst.DB_TABLE_HISTORY,
                    projection,
                    selection,
                    selectionArgs,
                    null, null, sortOrder, null);
        	break;
        }
        case ITEMS:{
            c = db.query(MLConst.DB_TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null, null, sortOrder, null);
            break;
        }
        case DRUGITEMS:{
            c = db.query(MLConst.DB_TABLE_DRUGLIST,
                    projection,
                    selection,
                    selectionArgs,
                    null, null, sortOrder, null);
            break;
        }
        case DRUGHISTORYS:{
            c = db.query(MLConst.DB_TABLE_DRUGHISTORY,
                    projection,
                    selection,
                    selectionArgs,
                    null, null, sortOrder, null);
            break;
        }
        default:{
            if(DBG) Log.e(TAG, "uri error in query()!!!");
            break;
        }
        }
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        int count = 0;
        switch(uriMatcher.match(uri)){
        case HISTORYS:{
            count = db.update(MLConst.DB_TABLE_HISTORY,
                    values,
                    selection,
                    selectionArgs);
            break;
        }
        case ITEMS:{
            count = db.update(MLConst.DB_TABLE_NAME,
                    values,
                    selection,
                    selectionArgs);
            break;
        }
        case DRUGITEMS:{
            count = db.update(MLConst.DB_TABLE_DRUGLIST,
                    values,
                    selection,
                    selectionArgs);
            break;
        }
        case DRUGHISTORYS:{
            count = db.update(MLConst.DB_TABLE_DRUGHISTORY,
                    values,
                    selection,
                    selectionArgs);
            break;
        }
        default:{
            if(DBG) Log.e(TAG, "uri error in update()!!!");
            break;
        }
        }
        mResolver.notifyChange(uri, null);
        return count;
    }

    private static class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_SQL);
            db.execSQL(CREATE_HISTORY);
            db.execSQL(CREATE_DRUGHISTORY);
            db.execSQL(CREATE_DRUG);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + MLConst.DB_TABLE_NAME); 
            db.execSQL(CREATE_SQL);
            db.execSQL("DROP TABLE IF EXISTS " + MLConst.DB_TABLE_HISTORY);
            db.execSQL(CREATE_HISTORY);
            db.execSQL("DROP TABLE IF EXISTS " + MLConst.DB_TABLE_DRUGHISTORY);
            db.execSQL(CREATE_DRUGHISTORY);
            db.execSQL("DROP TABLE IF EXISTS " + MLConst.DB_TABLE_DRUGLIST);
            db.execSQL(CREATE_DRUG);
        }
        
    }
}
